package cn.dnui_xcz826.dnuifood_16110100826.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.dnui_xcz826.dnuifood_16110100826.R;
import cn.dnui_xcz826.dnuifood_16110100826.bean.CommentBean;
import cn.dnui_xcz826.dnuifood_16110100826.bean.ResultBean;
import cn.dnui_xcz826.dnuifood_16110100826.listener.onRetrofitListener;
import cn.dnui_xcz826.dnuifood_16110100826.model.CommentModel;


public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder> implements onRetrofitListener<ResultBean> {
    private int pos;
    private CommentModel commentModel;
    private Context context;
    private List<CommentBean> data;
    private LayoutInflater layoutInflater;

    public CommentAdapter(Context context, List data, int layoutResource) {
        this.context=context;
        this.data=data;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v = layoutInflater.from(viewGroup.getContext()).inflate(R.layout.comment_item, viewGroup, false);
        return new CommentAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(CommentAdapter.ViewHolder holder, final int position) {
        final CommentBean entity = (CommentBean) data.get(position);
        if (entity==null)
            return;
        ViewHolder viewholder = (ViewHolder) holder;
        viewholder.mycomment_orderid.setText("订单id："+entity.getOrder_id()+"");
        viewholder.mycomment_foodname.setText(entity.getFoodname());
        viewholder.mycomment_commenttime.setText(entity.getComment_time());
        viewholder.mycomment_content.setText(entity.getContent());
        viewholder.mycomment_shopname.setText("【" + entity.getShopname() + "】");
        viewholder.mycomment_alter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final EditText et = new EditText(context);
                et.setText(entity.getContent());
                new AlertDialog.Builder(context).setTitle("修改评论")
                        .setView(et)
                        .setPositiveButton("确定",new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog, int which) {
                                pos=position;
                                alterComment(entity.getItem_id()+"",et.getText().toString());
                            }
                        }).setNegativeButton("取消",null)
                        .show();
            }
        });
        viewholder.mycomment_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pos=position;
                deleteComment(entity.getItem_id()+"");
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView mycomment_foodname, mycomment_commenttime, mycomment_content,
                mycomment_shopname,mycomment_orderid;
        Button mycomment_alter, mycomment_delete;
        public ViewHolder(View itemView) {
            super(itemView);
            mycomment_orderid = (TextView) itemView.findViewById(R.id.myorder_orderid);
            mycomment_foodname = (TextView) itemView.findViewById(R.id.myorder_sum);
            mycomment_commenttime = (TextView) itemView.findViewById(R.id.mycomment_commenttime);
            mycomment_content = (TextView) itemView.findViewById(R.id.mycomment_content);
            mycomment_shopname = (TextView) itemView.findViewById(R.id.myorder_shopname);
            mycomment_alter = (Button) itemView.findViewById(R.id.mycomment_alter);
            mycomment_delete = (Button) itemView.findViewById(R.id.mycomment_delete);
        }
    }
    private void alterComment(String item_id, String et){
        commentModel=new CommentModel();
        commentModel.updateComment(item_id,et,this);
    }
    private void deleteComment(String item_id){
        commentModel=new CommentModel();
        commentModel.deleteComment(item_id,this);
    }
    @Override
    public void onSuccess(ResultBean status, int flag) {
        if(flag==18)// UPDATECOMMENT
        {
            if(status.getSuccess().equals("1"))
            {
                Toast.makeText(context,"修改成功", Toast.LENGTH_SHORT).show();
                notifyDataSetChanged();
            }
            else
            {
                Toast.makeText(context,"修改失败", Toast.LENGTH_SHORT).show();
            }
        }
        else
        {
            if(status.getSuccess().equals("1"))
            {
                removeData(pos);
                Toast.makeText(context,"删除成功", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(context,"删除失败", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(context,"网络错误", Toast.LENGTH_SHORT).show();
    }


    public void removeData(int position) {
        data.remove(position);
        notifyItemRemoved(position);
    }

}
